3 * A MemoizedCallable subclass that stores function return values
4 * in an instance property rather than APC or APCu.
6 class ArrayBackedMemoizedCallable
extends MemoizedCallable
{
9 protected function fetchResult( $key, &$success ) {
10 if ( array_key_exists( $key, $this->cache
) ) {
12 return $this->cache
[$key];
18 protected function storeResult( $key, $result ) {
19 $this->cache
[$key] = $result;
24 * PHP Unit tests for MemoizedCallable class.
25 * @covers MemoizedCallable
27 class MemoizedCallableTest
extends PHPUnit\Framework\TestCase
{
29 use MediaWikiCoversValidator
;
32 * The memoized callable should relate inputs to outputs in the same
33 * way as the original underlying callable.
35 public function testReturnValuePassedThrough() {
36 $mock = $this->getMockBuilder( stdClass
::class )
37 ->setMethods( [ 'reverse' ] )->getMock();
38 $mock->expects( $this->any() )
40 ->will( $this->returnCallback( 'strrev' ) );
42 $memoized = new MemoizedCallable( [ $mock, 'reverse' ] );
43 $this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) );
47 * Consecutive calls to the memoized callable with the same arguments
48 * should result in just one invocation of the underlying callable.
50 * @requires extension apcu
52 public function testCallableMemoized() {
53 $observer = $this->getMockBuilder( stdClass
::class )
54 ->setMethods( [ 'computeSomething' ] )->getMock();
55 $observer->expects( $this->once() )
56 ->method( 'computeSomething' )
57 ->will( $this->returnValue( 'ok' ) );
59 $memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] );
61 // First invocation -- delegates to $observer->computeSomething()
62 $this->assertEquals( 'ok', $memoized->invoke() );
64 // Second invocation -- returns memoized result
65 $this->assertEquals( 'ok', $memoized->invoke() );
69 * @covers MemoizedCallable::invoke
71 public function testInvokeVariadic() {
72 $memoized = new MemoizedCallable( 'sprintf' );
74 $memoized->invokeArgs( [ 'this is %s', 'correct' ] ),
75 $memoized->invoke( 'this is %s', 'correct' )
80 * @covers MemoizedCallable::call
82 public function testShortcutMethod() {
85 MemoizedCallable
::call( 'sprintf', [ 'this is %s', 'correct' ] )
90 * Outlier TTL values should be coerced to range 1 - 86400.
92 public function testTTLMaxMin() {
93 $memoized = new MemoizedCallable( 'abs', 100000 );
94 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
96 $memoized = new MemoizedCallable( 'abs', -10 );
97 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
101 * Closure names should be distinct.
103 public function testMemoizedClosure() {
104 $a = new MemoizedCallable( function () {
108 $b = new MemoizedCallable( function () {
112 $this->assertEquals( $a->invokeArgs(), 'a' );
113 $this->assertEquals( $b->invokeArgs(), 'b' );
115 $this->assertNotEquals(
116 $this->readAttribute( $a, 'callableName' ),
117 $this->readAttribute( $b, 'callableName' )
120 $c = new ArrayBackedMemoizedCallable( function () {
123 $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
127 * @expectedExceptionMessage non-scalar argument
128 * @expectedException InvalidArgumentException
130 public function testNonScalarArguments() {
131 $memoized = new MemoizedCallable( 'gettype' );
132 $memoized->invoke( new stdClass() );
136 * @expectedExceptionMessage must be an instance of callable
137 * @expectedException InvalidArgumentException
139 public function testNotCallable() {
140 $memoized = new MemoizedCallable( 14 );